package com.yan.deezer.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.yan.deezer.R;
import com.yan.deezer.ui.fragement.interaction.DeezerFragmentInterface;
import com.yan.deezer.ui.fragment.AlbumTrackListFragment;
import com.yan.deezer.ui.fragment.ArtistAlbumListFragment;
import com.yan.deezer.ui.fragment.ArtistListFragment;
import com.yan.deezer.ui.fragment.DeezerHomeFragment;

public class DeezerDemoActivity extends SherlockFragmentActivity implements
		DeezerFragmentInterface {
	static int mStackLevel = 1;
	protected static DeezerFragmentInterface deezerFragmentInterface;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_stack);
		deezerFragmentInterface = this;

		if (savedInstanceState == null) {
			// Do first time initialization -- add initial fragment: Deezer's Home Page, this time we don't need animation
			addDeezerHomeFragmentToStack();
			
		} else {
			mStackLevel = savedInstanceState.getInt("level");
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		
		getSupportMenuInflater().inflate(R.menu.search_action_provider, menu);//This is the action bar for searching artists

		return true;

	}
	
	
	public void addDeezerHomeFragmentToStack() {

		Fragment newFragment = DeezerHomeFragment.newInstance(mStackLevel);
		FragmentTransaction ft = getSupportFragmentManager()
				.beginTransaction();
		ft.add(R.id.simple_fragment, newFragment).commit();
	}
	
	

	@Override
	public void addArtistAlbumListFragmentToStack(String artistID) {

		mStackLevel++;

		// Instantiate a new fragment.
		Fragment newFragment = ArtistAlbumListFragment.newInstance(mStackLevel,
				artistID);

		// Add the fragment to the activity, pushing this transaction
		// on to the back stack.
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_left_exit,
				R.anim.fragment_slide_right_enter,
				R.anim.fragment_slide_right_exit);
		ft.replace(R.id.simple_fragment, newFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void addArtistListFragmentToStack(String userInput) {

		mStackLevel++;
		// Instantiate a new fragment.
		Fragment newFragment = ArtistListFragment.newInstance(mStackLevel,
				userInput);

		// Add the fragment to the activity, pushing this transaction
		// on to the back stack.
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_left_exit,
				R.anim.fragment_slide_right_enter,
				R.anim.fragment_slide_right_exit);
		ft.replace(R.id.simple_fragment, newFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void addAlbumTrackFragmentToStack(String albumID) {

		mStackLevel++;
		// Instantiate a new fragment.
		Fragment newFragment = AlbumTrackListFragment.newInstance(mStackLevel,
				albumID);

		// Add the fragment to the activity, pushing this transaction
		// on to the back stack.
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_left_exit,
				R.anim.fragment_slide_right_enter,
				R.anim.fragment_slide_right_exit);
		ft.replace(R.id.simple_fragment, newFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	public static DeezerFragmentInterface getDeezerFragmentInterface() {

		return deezerFragmentInterface;
	}

}